package com.cchip.cloud;

import java.sql.Time;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Chronometer;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.cchip.cloud.api.RsApi;
import com.cchip.cloud.db.MeetingDb;
import com.cchip.cloud.system.Meeting;

public class MeetingActivity extends Activity {

	private TextView mRoomIdTxt = null;
	private TextView mRoomNameTxt = null;
	private TextView mStartTimeTxt = null;
	private Chronometer mDurationChr = null;
	private CheckBox mMuteCbx = null;
	private Button mLeaveBtn = null;

	private Activity context = null;
	private RsApi rsApi = RsApi.getInstance();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		context = this;
		setContentView(R.layout.meeting);

		rsApi.dial(Meeting.current.roomId);
		Meeting.inMeeting = true;

		mRoomIdTxt = (TextView) findViewById(R.id.room_id);
		mRoomNameTxt = (TextView) findViewById(R.id.room_name);
		mStartTimeTxt = (TextView) findViewById(R.id.start_time);
		mDurationChr = (Chronometer) findViewById(R.id.duration);
		mMuteCbx = (CheckBox) findViewById(R.id.mute);
		mLeaveBtn = (Button) findViewById(R.id.leave);

		mMuteCbx.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				rsApi.setMute(isChecked);
			}

		});

		mLeaveBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				rsApi.hangup();
				Meeting.inMeeting = false;
				context.finish();
			}
		});

	}

	@Override
	protected void onStart() {
		super.onStart();

		MeetingDb.getInstance(this).addLog(Meeting.current);

		mRoomIdTxt.setText(Meeting.current.roomId);
		mRoomNameTxt.setText(Meeting.current.roomName);
		Time time = new Time(Meeting.current.startTime);
		mStartTimeTxt.setText(time.toString());
		mDurationChr.setBase(Meeting.current.baseTime);
		mDurationChr.start();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK)
			return true;
		else
			return super.onKeyDown(keyCode, event);
	}

}
